IF exists(SELECT * FROM dbo.sysobjects WHERE name='schooljaar' AND xtype='FN') 
BEGIN
	DROP FUNCTION schooljaar
END
GO

CREATE function schooljaar(@datum datetime)
returns varchar(4)
as
begin
  declare @jaar varchar(4)
  if month(@datum) < 9 set @jaar = convert(varchar(4),year(dateAdd(yy,-1,@datum)))
  else 	set @jaar =  convert(varchar(4),year(@datum))  
  return @jaar
end